package com.tongfusoft.document.web.controller.doc;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.document.doc.service.IAdvancedSearchService;
import com.tongfusoft.document.doc.service.IDocService;
import com.tongfusoft.document.entity.CustomSearches;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.dictionary.service.IDocumentDictionaryService;
import com.tongfusoft.document.manager.metadata.service.IMetadataService;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;

@Controller
public class CustomSearchController extends BaseController{
	@Autowired
	public IAdvancedSearchService advancedSearchService;
	@Autowired
	@Qualifier("documentDictionaryService")
	public IDocumentDictionaryService dDictionaryService;
	
	@Autowired
	@Qualifier("metaDataManagerService")
	public IMetadataService mMService;
	
	@Autowired
	public IDocService docService;
	
	@RequestMapping("/customSearch")
	@UrlDesc("自定义查询页")
	public String customSearchPage(Model model,String csId) {

		model.addAttribute("mainPageKey", "customSearch");
		model.addAttribute("csId", csId);
		model.addAttribute("mainPage", "doc/customSearch");
		model.addAttribute("docTypeList", this.dDictionaryService.getDictByCode("docType"));
		model.addAttribute("groupList", this.docService.docEditGroupByUser(((Users)super.getUser()).getUserId()));
		model.addAttribute("metaDateList", this.mMService.getIsEnableMeta());
		return "base";

	}
	
	@UrlDesc("修改自定义查询条件页")
	@RequestMapping("/customSearch/editPage/{csId}")
	public String advancedPage(Model model,@PathVariable("csId") String csId ) {
		
		
		model.addAttribute("mainPageKey", "editCriteria");
		model.addAttribute("mainPage", "doc/editCriteria");
		CustomSearches cs = this.advancedSearchService.getDetail(csId, (Users)super.getUser());
		model.addAttribute("csId", csId);
		model.addAttribute("queryTa", cs!=null?cs.getCsData():"");
		model.addAttribute("csName", cs!=null?cs.getCsName():"");
		model.addAttribute("docTypeList", this.dDictionaryService.getDictByCode("docType"));
		model.addAttribute("groupList", this.docService.docEditGroupByUser(((Users)super.getUser()).getUserId()));
		model.addAttribute("metaDateList", this.mMService.getIsEnableMeta());
		return "base";
		
		
	}
	
	
	

	
	@UrlDesc("获取自定义查询条件列表")
	@RequestMapping("/queryCriteria/list")
	@ResponseBody
	public ResultModel myDocPage(Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try{
			rm.setData(this.advancedSearchService.queryCriteriaList((Users)super.getUser()));
		}catch(Exception e){
			
			e.printStackTrace();
			rm.setState("error");
		}
		
		return rm;

	}
	
	@UrlDesc(value="删除自定义查询条件列表")
	@RequestMapping("/queryCriteria/delete")
	@ResponseBody
	public ResultModel delete(Model model,String csId) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try{
			
			
			CustomSearches cs = new CustomSearches();
			cs.setCsId(csId);
			
			this.advancedSearchService.deleteSearchTa(cs, (Users)super.getUser());
		}catch(Exception e){
			
			e.printStackTrace();
			rm.setState("error");
		}
		
		return rm;

	}
	
}
